<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
      <el-form-item label="" prop="id">
        <el-input v-model="dataForm.id" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="imei" prop="imei">
        <el-input v-model="dataForm.imei" placeholder="imei"></el-input>
      </el-form-item>
      <el-form-item label="报警类型" prop="type">
        <el-input v-model="dataForm.type" placeholder="报警类型"></el-input>
      </el-form-item>
      <el-form-item label="报警内容" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="报警内容"></el-input>
      </el-form-item>
      <el-form-item label="" prop="level">
        <el-input v-model="dataForm.level" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="发生时间" prop="occurTime">
        <el-input v-model="dataForm.occurTime" placeholder="发生时间"></el-input>
      </el-form-item>
      <el-form-item label="最新一次上报时间" prop="endTime">
        <el-input v-model="dataForm.endTime" placeholder="最新一次上报时间"></el-input>
      </el-form-item>
      <el-form-item label="第一次上报位置" prop="longitude">
        <el-input v-model="dataForm.longitude" placeholder="第一次上报位置"></el-input>
      </el-form-item>
      <el-form-item label="" prop="latitude">
        <el-input v-model="dataForm.latitude" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="报警次数" prop="occurNum">
        <el-input v-model="dataForm.occurNum" placeholder="报警次数"></el-input>
      </el-form-item>
      <el-form-item label="" prop="companyId">
        <el-input v-model="dataForm.companyId" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="是否处理，1已处理" prop="processStatus">
        <el-input v-model="dataForm.processStatus" placeholder="是否处理，1已处理"></el-input>
      </el-form-item>
      <el-form-item label="" prop="processUser">
        <el-input v-model="dataForm.processUser" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="processRemark">
        <el-input v-model="dataForm.processRemark" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="attributes">
        <el-input v-model="dataForm.attributes" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useAlarmApi, useAlarmSubmitApi } from '@/api/gps/alarm'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
  id: '',
  imei: '',
  type: '',
  remark: '',
  level: '',
  occurTime: '',
  endTime: '',
  longitude: '',
  latitude: '',
  occurNum: '',
  companyId: '',
  processStatus: 0,
  processUser: '',
  processRemark: '',
  attributes: ''
})

const init = (id?: number) => {
  visible.value = true
  dataForm.id = ''

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields()
  }
  if (id) {
    getAlarm(id)
  }
}

const getAlarm = (id: number) => {
  useAlarmApi(id).then((res) => {
    Object.assign(dataForm, res.data)
  })
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false
    }
    useAlarmSubmitApi(dataForm).then(() => {
      ElMessage.success({
        message: '操作成功',
        duration: 500,
        onClose: () => {
          visible.value = false
          emit('refreshDataList')
        }
      })
    })
  })
}

defineExpose({
  init
})
</script>
